﻿using HandyControl.Tools.Extension;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AutoOiSystem.Models
{
    public class MainModel:BaseNotifyPropertyChanged
    {
        public int AreaId;
        string tempArea;
        string ApOiPath = string.Empty;
        string LtOiPath = string.Empty;
        string AllPath = string.Empty;
        int verNum = 0;

        public MainModel()
        {
            try
            {
                AllPath = ConfigurationManager.AppSettings["PATH"];
                tempArea = ConfigurationManager.AppSettings["AREA"];
                ApOiPath = ConfigurationManager.AppSettings["ApPath"];
                LtOiPath = ConfigurationManager.AppSettings["LtPath"];
                verNum= int.Parse(ConfigurationManager.AppSettings["VER"]);
            }
            catch { }
            
        }

        private string _areaName;
        public string AreaName
        {
            get
            {
               
                return _areaName;
            }
            set
            {
                if (_areaName != value)
                {
                    _areaName = value;
                    RaisePropertyChanged("AreaName");
                }

            }
        }



        private string _logText;
        public string LogText
        {
            get { return _logText; }
            set
            {
                if (_logText != value)
                {
                    _logText = value;
                    RaisePropertyChanged("LogText");
                }

            }
        }
        //public ControlModel()
        //{
        //    ScanText = "asdad";
        //}

        private bool _isFocus;
        public bool IsFocus
        {
            get { return _isFocus; }
            set
            {
                //if (_isFocus != value)
                {
                    _isFocus = value;
                    RaisePropertyChanged("IsFocus");
                }

            }
        }


        private string _scanText;
        public string ScanText
        {
            get { return _scanText; }
            set
            {
                if (_scanText != value)
                {
                    _scanText = value;
                    RaisePropertyChanged("ScanText");
                }

            }
        }

        private string _oiPath;
        public string oiPath
        {
            get
            {
                //var station = ConfigurationManager.AppSettings["PATH"];

                //if (station != null) { _oiPath = station; }

                return _oiPath;
            }
            set
            {
                if (_oiPath != value)
                {
                    _oiPath = value;
                    RaisePropertyChanged("oiPath");
                }

            }
        }



        private string _selectedItem;
        public string SelectedItem
        {
            get
            {
                if (_selectedItem == null)
                { _selectedItem = "Others"; return _selectedItem; }
                else 
                return _selectedItem;
            }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    LogText += DateTime.Now.ToString() + "：选择站号：" + value + "\r\n";
                    RaisePropertyChanged("SelectedItem");


                    //IsFocus = false;
                    IsFocus = true;

                }
            }
        }



        public List<string> StationNames
        {
            get
            {
                var station = ConfigurationManager.AppSettings["StationNum"];
                if (station != null)
                {
                    string tempStation = station;


                    string[] StationNames = tempStation.Split('-');
                    if (StationNames.Length > 0)
                    {
                        List<string> tempList
                            = new List<string>();

                        tempList.Add("Others");
                        

                        for (int i = 0; i < StationNames.Length; i++)
                        {
                            tempList.Add(StationNames[i]);
                        }

                        tempList.Add("Main");


                        return tempList;
                    }

                }

                return null;



            }
        }



        public List<string> AreaNames
        {
            get
            {
                string[] areaNames=new string[2] { "AP","LT"};
                //var  = ConfigurationManager.AppSettings["AREA"];
              
                  
                    if (areaNames.Length > 0)
                    {
                        return areaNames.ToList();
                    }
                    else
                    return null;

                //return null;



            }
        }


        private string _selectedAreaItem;
        public string SelectedAreaItem
        {
            get
            {
                if (_selectedAreaItem != null)
                    return _selectedAreaItem;
                else
                {
                    if (tempArea != null)
                    {
                        if (tempArea.ToUpper() == "AP")
                        {
                            AreaId = 1;

                            if (verNum == 0)
                            { oiPath = AllPath; }
                            else
                            { oiPath = ApOiPath; }
                        }
                        else
                        {
                            AreaId = 2;

                            if (verNum == 0)
                            { oiPath = AllPath; }
                            else
                            { oiPath = LtOiPath; }
                        }

                        _selectedAreaItem = tempArea;

                        AreaName = "OiSystem V1.0-" + _selectedAreaItem;
                    }

                    return _selectedAreaItem;
                }

             
            }
            set
            {
                //if (_selectedAreaItem != value)
                //{
                _selectedAreaItem = value;


                if (value != null)
                {
                    if (value.ToUpper() == "AP")
                    {
                        AreaId = 1;

                        if (verNum == 0)
                        { oiPath = AllPath; }
                        else
                        { oiPath = ApOiPath; }

                        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                        config.AppSettings.Settings["AREA"].Value= value.ToUpper();
                        config.Save(ConfigurationSaveMode.Modified);
                        //刷新，否则程序读取的还是之前的值（可能已装入内存）
                        ConfigurationManager.RefreshSection("appSettings");

                    }
                    else
                    {
                        AreaId = 2;

                        if (verNum == 0)
                        { oiPath = AllPath; }
                        else
                        { oiPath = LtOiPath; }

                        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                        config.AppSettings.Settings["AREA"].Value = value.ToUpper();
                        config.Save(ConfigurationSaveMode.Modified);
                        //刷新，否则程序读取的还是之前的值（可能已装入内存）
                        ConfigurationManager.RefreshSection("appSettings");
                    }

                    AreaName = "OiSystem V1.0-" + value;
                }

                LogText += DateTime.Now.ToString() + "：选择区域号：" + value + "\r\n";
                RaisePropertyChanged("SelectedAreaItem");

                //IsFocus = false;
                IsFocus = true;
            }
        }

    }
}
